package com.demo.delivery;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

public class OrderActivity extends Activity {
	private ProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_customer);

		TableLayout table = (TableLayout) findViewById(R.id.Order);

		Intent i = getIntent();
		setTitle("Card No. " + i.getStringExtra("card_no"));

		TextView name = (TextView) table.findViewById(R.id.name);
		name.setText(i.getStringExtra("client_name"));

		TextView address = (TextView) table.findViewById(R.id.address);
		address.setText(i.getStringExtra("address"));

		TextView mobile = (TextView) table.findViewById(R.id.mobile);
		mobile.setText(i.getStringExtra("mobile"));

		TextView bf = (TextView) table.findViewById(R.id.breakfast_quantity);
		bf.setText(i.getStringExtra("breakfast_quantity"));

		TextView lunch = (TextView) table.findViewById(R.id.lunch_quantity);
		lunch.setText(i.getStringExtra("lunch_quantity"));

		TextView dinner = (TextView) table.findViewById(R.id.dinner_quantity);
		dinner.setText(i.getStringExtra("dinner_quantity"));

		TextView amount = (TextView) table.findViewById(R.id.amount);
		amount.setText(i.getStringExtra("amount"));

		String collector = i.getStringExtra("collector") ;
		if((collector)!= null)
		{
		EditText c = (EditText) table
				.findViewById(R.id.deliveredTo);
		c.setText(collector);
		c.setEnabled(false);
		c.setFocusable(false);
		}
		else
		{
			EditText c = (EditText) table
					.findViewById(R.id.deliveredTo);
			
			c.setEnabled(true);
			c.setFocusable(true);
			
		}
	
		
		String collection = i.getStringExtra("collection") ;
		if((collection)!= null)
		{
		EditText c = (EditText) table
				.findViewById(R.id.collection);
		c.setText(collection);
		
		c.setEnabled(false);
		c.setFocusable(false);
		Button button = (Button) findViewById(R.id.submit);
		button.setVisibility(View.GONE);
		Button button2 = (Button) findViewById(R.id.fail);
		button2.setVisibility(View.GONE);
		}
		else
		{
			Button button = (Button) findViewById(R.id.submit);
			button.setVisibility(View.VISIBLE);
			Button button2 = (Button) findViewById(R.id.fail);
			button2.setVisibility(View.VISIBLE);
			EditText c = (EditText) table
					.findViewById(R.id.collection);
			
			c.setEnabled(true);
			c.setFocusable(true);
		}
		
		Button button = (Button) findViewById(R.id.submit);

		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				TableLayout table = (TableLayout) findViewById(R.id.Order);
				EditText collection = (EditText) table
						.findViewById(R.id.collection);
				EditText collector = (EditText) table
						.findViewById(R.id.deliveredTo);

				if (collection.getText().length() == 0)
					return;

				// show progress dialog
				// dialog = ProgressDialog.show(getApplicationContext(), "",
				// "Loading...");

				String json = "";
				// 3. build jsonObject
				JSONObject jsonObject = new JSONObject();
				try {

					jsonObject.accumulate("amount_collected",
							collection.getText());
					jsonObject.accumulate("delivery_accepted_by",
							collector.getText());
					jsonObject.accumulate("delivery_status", "Delivered");
					jsonObject.accumulate("filename", "");

				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Intent i = getIntent();

				String id = i.getStringExtra("id");
				String url = "http://qrcode.educroc.com/order_zones/" + id;
				GetJsonTask task = new GetJsonTask(url, jsonObject);
				task.execute();

				Intent intent = new Intent(getApplicationContext(),
						PendingOrdersActivity.class);
    			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

				startActivity(intent);

			}

		});

		Button fail = (Button) findViewById(R.id.fail);

		fail.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				
				String json = "";
				// 3. build jsonObject
				JSONObject jsonObject = new JSONObject();
				try {

					jsonObject.accumulate("amount_collected",
							"null");
					jsonObject.accumulate("delivery_accepted_by",
							"null");
					jsonObject.accumulate("delivery_status", "Failed");
					jsonObject.accumulate("filename", "");

				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				Intent i = getIntent();

				String id = i.getStringExtra("id");
				String url = "http://qrcode.educroc.com/order_zones/" + id;
				GetJsonTask task = new GetJsonTask(url, jsonObject);
				task.execute();

				
				TableLayout table = (TableLayout) findViewById(R.id.Order);

				Intent intent = new Intent(getApplicationContext(),
						PendingOrdersActivity.class);
    			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );

				startActivity(intent);

			}
		});
	}
}
